/** 
 * FtpFileIconCache.java
 * @author Lex
 * 2007-9-6 ����01:41:39
 */
package org.lex.ftp.util;

import java.io.File;

import javax.swing.Icon;
import javax.swing.filechooser.FileSystemView;

import org.lex.ftp.FTP;
import org.lex.io.filesystem.FileIconCache;
import org.lex.io.filesystem.FileNameUtilities;
import org.lex.net.ftp.IFTPFile;


public class FTPFileIconCache {
	private static final FTPFileIconCache instance = new FTPFileIconCache();

	private FileIconCache cache = null;
	private Icon folderIcon = null;

	public static FTPFileIconCache getInstance() {
		return instance;
	}

	private FTPFileIconCache() {
		File dir = new File(FTP.getApplication().getContext().getLocalStorage().getApplicationHome(), "cache");
		cache = new FileIconCache(dir, "icons");
		folderIcon = FileSystemView.getFileSystemView().getSystemIcon(new File("."));
	}

	public Icon getFolderIcon() {
		return this.folderIcon;
	}

	public Icon getIcon(IFTPFile file) {
		if (true == file.isDirectory())
			return folderIcon;
		return cache.get(FileNameUtilities.getExtension(file.getName(), '/'));
	}

	public Icon getIcon(String extension) {
		if (null == extension) {
			return folderIcon;
		} else {
			return cache.get(extension);
		}
	}
}
